<template>
  <div class="login">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>后台管理系统</span>
      </div>
      <el-form label-width="80px" :model="form" ref="form">
        <el-form-item
          label="邮箱"
          prop="userName"
          :rules="[
            { required: true, message: '请输入邮箱', trigger: 'blur ' },
            {
              min: 2,
              max: 12,
              message: '长度在2-12位字符之间',
              trigger: 'blur',
            },
          ]"
        >
          <el-input v-model="form.userName"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="passWord"
          :rules="[
            { required: true, message: '请输入密码', trigger: 'blur ' },
            {
              min: 6,
              max: 14,
              message: '长度在6-14位字符之间',
              trigger: 'blur',
            },
          ]"
        >
          <el-input type="password" v-model="form.passWord"></el-input>
        </el-form-item>
        <el-button type="primary" @click="login('form')">登录</el-button>
        <el-button type="info" @click="enroll">注册</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loginFunc } from "@/utils/http.js";
export default {
  name: "LoginView",
  data() {
    return {
      form: {
        userName: "",
        passWord: "",
      },
    };
  },
  methods: {
    enroll() {
      this.$router.push("/enroll");
    },
    login(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          // console.log(this.form);
          let { userName, passWord } = this.form;
          let user = userName;
          loginFunc("/user/login", {
            user,
            passWord,
          }).then((data) => {
            // 登录成功
            if (data.code == 200) {
              // console.log(data);
              let { email, userName, avatar, id, power } = data;
              this.$store.dispatch("user/setEmailAsync", email);
              this.$store.dispatch("user/setAvatarAsync", avatar);
              this.$store.dispatch("user/setUserNameAsync", userName);
              this.$store.dispatch("user/setIdAsync", id);
              this.$store.dispatch("user/setPowerAsync", power);
              // 跳转
              this.$message({
                message: "登录成功",
                type: "success",
              });
              this.$router.push("/home");
            } else {
              this.$message({
                message: "登录失败!请检查账号或密码",
                type: "error",
              });
            }
          });
        } else {
          return;
        }
      });
    },
  },
  components: {},
};
</script>

<style lang='scss'>
.login {
  width: 100%;
  height: 100%;
  position: absolute;
  background: url("../assets/bgc.webp") no-repeat center;
  background-size: 100%;
  .box-card {
    width: 450px;
    height: 270px;
    margin: 200px auto;
    text-align: center;
    background-color: rgba(255, 255, 255, 0.1);
    color: #fff;
    .el-form-item__label {
      color: #fff;
    }
  }
}
</style>
